﻿package app.view.components.sections.russia 
{
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import gs.TweenLite;
	
	/**
	 * 3.4.2.1 ЧУДЕСА ПРИРОДЫ (в виде ленты)
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@yandex.ru
	 */
	public class RussiaOneNatureDBase extends LabeledSectionBase
	{
		// app.view.components.sections.russia.RussiaOneNatureDBase
		
		public var narrow_left:NarrowD;
		public var narrow_right:NarrowD;
		public var textEvent:TextField;
		public var bgdImage:Sprite;Sprite
		private var _currentId:int;
		private var _isLast:Boolean;
		private var _isNext:Boolean;
		
		private static const TIME_SHOW_NAVIGATE:Number = 0.3;
		private static const TIME_HIDE_NAVIGATE:Number = 0.2;
		private var _arrayDataItem:Array;
		private var _image:Image;
		
		//	CONSTRUCTOR
		public function RussiaOneNatureDBase() 
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			
			_isLast = false;
			_isNext = false;
			narrow_left.alpha = 0;
			narrow_right.alpha = 0;
			narrow_left.selected = true;
			narrow_right.selected = true;
			
			textEvent.wordWrap = true;
			textEvent.multiline= true;
			textEvent.text = '';
			
			_image = new Image(bgdImage.width, bgdImage.height, Image.RESIZE_MIN, Image.ALIGN_CENTER);
			addChild(_image);
			_image.x = bgdImage.x;
			_image.y = bgdImage.y;
		}
		
		private function setEvents():void
		{
			
		}
		
		private function removeEvents():void
		{
			narrow_left.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
			narrow_right.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			_image.destroy();
			removeChild(_image);
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			////trace('data!!!+++', _data.data);
			
			_arrayDataItem = new Array();
			
			
			var ind:int = 0;
			for each(var object:* in _data.params.data.data.image_text)
			{
				var obj:Object = new Object();
				//trace('name',object.name);
				//trace('year',object.text[1]);
				//trace('city',object.text[2]);
				//trace('country',object.text[3]);
				//trace('text',object.text[4]);
				//trace('image',object.image[1].image_path);
				
				obj.id = ind;
				obj.name = object.name;
				obj.text = String(object.text).substr(1);
				object.image[1].image_path!='' ? obj.image=RemoteProxy.DOMEN+object.image[1].image_path : obj.image=''
				
				
				//trace('----------------------');
				_arrayDataItem.push(obj);
				ind++;
			}
			
			_currentId = _data.params.selectedId;
			
			setContentParam(_currentId);
			
			checkedNavigation();
			
			
			setEvents();
		};
		
		private function checkedNavigation():void
		{
			// проверяем навигацию
			// проверка
			////trace('checkedNavigation---+++',_currentId);
			if (_currentId>=1) 
			{
				TweenLite.to(narrow_left, TIME_HIDE_NAVIGATE, { alpha:1 } );
				narrow_left.addEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = true;
				narrow_left.selected = !_isLast;
			}else
			{
				narrow_left.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = false;
				narrow_left.selected = !_isLast;
				TweenLite.to(narrow_left, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
			if (_currentId<_arrayDataItem.length-1) 
			{
				TweenLite.to(narrow_right, TIME_HIDE_NAVIGATE, { alpha:1 } );
				narrow_right.addEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = true;
				narrow_right.selected = !_isNext;
			}else
			{
				narrow_right.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = false;
				narrow_right.selected = !_isNext;
				TweenLite.to(narrow_right, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
		}
		
		
		/**
		 * установим нужные параметры сонтенту
		 */
		private function setContentParam(n:int):void
		{
			trace('set_ContentParam = ', n);
			
			setParamHead(HeadMarkerBase.BGD_GREEN, _arrayDataItem[_currentId].name);
			textEvent.htmlText = _arrayDataItem[n].text;
			_image.loadImage(_arrayDataItem[_currentId].image);
			
		}
		
		/**
		 * предыдущее событие
		 * @param	e
		 */
		private function onClickLeftBtn(e:MouseEvent):void 
		{
			_currentId = _currentId - 1;
			setContentParam(_currentId);
			checkedNavigation();
		}
		
		/**
		 * следущее события
		 * @param	e
		 */
		private function onClickRightBtn(e:MouseEvent):void 
		{
			_currentId = _currentId + 1;
			setContentParam(_currentId);
			checkedNavigation();
		}
	}

}